package com.witmore.neutron.ai.web.wechat.service.impl;

import com.jk.common.response.ResponsePageResult;
import com.jk.common.response.ResponseResult;
import com.witmore.neutron.ai.web.filter.GlobalAccountSession;
import com.witmore.neutron.ai.web.wechat.client.McpStockAbnormalRecordClient;
import com.witmore.neutron.ai.web.wechat.dto.StockAbnormalRecordDTO;
import com.witmore.neutron.ai.web.wechat.dto.StockAbnormalRecordQueryDTO;
import com.witmore.neutron.ai.web.wechat.service.StockAbnormalRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 股票异动记录服务实现类
 * Author: 豆子高
 * Date: 2025/12/19
 */
@Slf4j
@Service
public class StockAbnormalRecordServiceImpl implements StockAbnormalRecordService {
    @Autowired
    private McpStockAbnormalRecordClient mcpStockAbnormalRecordClient;

    @Override
    public ResponsePageResult<StockAbnormalRecordDTO> queryAbnormalRecords(StockAbnormalRecordQueryDTO queryDTO) {
        Integer userId = GlobalAccountSession.getUserInfo() != null ? GlobalAccountSession.getUserInfo().getScorpionUserId() : null;
        if (userId == null) {
            log.error("用户未登录，无法查询异动记录");
            return ResponsePageResult.fail(500, "用户信息异常");
        }

        try {
            log.info("Web服务分页查询异动记录，调用MCP服务: userId={}, queryDTO={}", userId, queryDTO);

            // 调用MCP服务分页查询
            return mcpStockAbnormalRecordClient.queryAbnormalRecords(
                    userId.longValue(),
                    queryDTO.getStockCode(),
                    queryDTO.getStockName(),
                    queryDTO.getAbnormalTypeCode(),
                    queryDTO.getStartDate(),
                    queryDTO.getEndDate(),
                    queryDTO.getPage(),
                    queryDTO.getSize()
            );

        } catch (Exception e) {
            log.error("查询股票异动记录失败", e);
            return ResponsePageResult.fail(500, "查询失败：" + e.getMessage());
        }
    }

    @Override
    public ResponseResult<StockAbnormalRecordDTO> getAbnormalRecordById(Long id) {
        try {
            log.info("Web服务获取异动记录，调用MCP服务: recordId={}", id);

            // 调用MCP服务获取记录
            return mcpStockAbnormalRecordClient.getAbnormalRecordById(id);
        } catch (Exception e) {
            log.error("获取异动记录详情失败", e);
            return null;
        }
    }

    @Override
    public Object getAlertDetail(Integer alertId) {
        return mcpStockAbnormalRecordClient.getAlertDetail(alertId);
    }

}
